package com.ma.controller;

import com.ma.component.PhoneRandomBuilder;
import com.ma.redis.StringRedisServiceImpl;
import com.ma.utils.IntersmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * author: MaHX
 * date: 2019/5/30/22:11
 * description: 注册获取验证码
 **/
@RestController
public class GetPhoneCodeController {
    @Autowired
    StringRedisServiceImpl stringRedisService;

    @PostMapping("/getcode")
    public int getAuthCode(HttpServletRequest request){
        String phone = request.getParameter("phone");
//        String sign = request.getParameter("sign");
        String trueMsgCode = PhoneRandomBuilder.fourRandomBuilder();
        System.out.println("手机验证码："+trueMsgCode);

        stringRedisService.set(phone,trueMsgCode);
        stringRedisService.expire(phone,300000);

        String msgCode = "验证码为:"+trueMsgCode;

        IntersmsUtil intersmsUtil = new IntersmsUtil();
        try{
            intersmsUtil.SendSms("86"+phone,trueMsgCode,msgCode);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }
}
